namespace hw1
{
    public partial class homework : Form
    {
        private int count = 1;

        private int[] array = new int[20];

        public homework()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int max = array.Max();
            int min = array.Min();
            int sum = array.Sum();
            double aver = sum/count;
            this.tbxContent.Clear();
            this.tbxContent.AppendText("最大值为:" + max + Environment.NewLine);
            this.tbxContent.AppendText("最小值为:" + min + Environment.NewLine);
            this.tbxContent.AppendText("平均值为:" + aver + Environment.NewLine);
            this.tbxContent.AppendText("数组和为：" + sum);
        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.textArrayValue.Text.ToString() != "")
            {
                array[count] = Convert.ToInt32(this.textArrayValue.Text.ToString()); 
                this.tbxArray.AppendText("数组第" + count + "个值为:" + this.textArrayValue.Text.ToString()+Environment.NewLine);
                this.textArrayValue.Clear();
                count++;
            }
            else
            {
                this.tbxContent.Clear();
                this.tbxContent.AppendText("请输入数字");
            }
        }

        private void tbxArray_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_2(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void btnCalculate2_Click(object sender, EventArgs e)
        {
            if(this.textMax.Text.ToString()!=""&&this.textMin.Text.ToString()!="")
            {
                int num = 0;
                int max = Convert.ToInt32(this.textMax.Text.ToString());
                int min = Convert.ToInt32(this.textMin.Text.ToString());
                for (int i = min; i <= max; i++)
                { 
                    if(isPrime(i))
                    {
                        this.tbxContent2.AppendText(i.ToString()+" ");
                        num++;
                        if(num==10)
                        {
                            this.tbxContent2.AppendText(Environment.NewLine);
                            num = 0;
                        }
                    }
                }
            }
            else
            {
                this.tbxContent2.AppendText("请正确输入数值为整数");
            }
        }
        private bool isPrime(int num)
        {
            //两个较小数另外处理
            if (num == 2 || num == 3)
                return true;
            //不在6的倍数两侧的一定不是质数
            if (num % 6 != 1 && num % 6 != 5)
                return false;
            int tmp = (int)Math.Sqrt(num);
            //在6的倍数两侧的也可能不是质数
            for (int i = 5; i <= tmp; i += 6)
                if (num % i == 0 || num % (i + 2) == 0)
                    return false;
            //排除所有，剩余的是质数
            return true;
        }
    }
}